<template>
  <el-row type="flex" justify="center">
    <el-form ref="formData" :model="formData" :rules="rules" label-width="80px" @keyup.enter.native="login(formData)">
      <el-form-item prop="account" label="用户名">
        <el-input v-model="formData.account" placeholder="请输入用户名" prefix-icon="icon-login_user"
                  clearable></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input v-model="formData.password" placeholder="请输入密码" type="password" prefix-icon="icon-login_pwd"
                  clearable></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="formData.role" placeholder="请选择你的身份">
          <el-option label="管理员" value="管理员"/>
          <el-option label="教师" value="教师"/>
          <el-option label="学生" value="学生"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="btn" @click="login(formData)" icon="el-icon-upload">登录</el-button>
        <el-button @click="resetForm('formData')">重置</el-button>
      </el-form-item>
      <router-link to="register">没有密码？注册</router-link>
    </el-form>
  </el-row>
</template>

<script>
import axios from 'axios'
import {userStore} from "@/store";

export default {
  data() {
    return {
      formData: {
        account: '',
        password: '',
        role: '',
        // ustore: userStore()
      },
      rules: {
        account: [{required: true, message: '用户名不能为空', trigger: 'blur'}],
        password: [{required: true, message: '密码不能为空', trigger: 'blur'}]
      },
      options: ["教师", "学生", "管理员"],

    };
  },
  methods: {
    login(formData) {
      const userstore = userStore()
      const that = this
      console.log("role:" + formData.role)
      if (formData.role === "管理员") {
        axios.get('/login/loginAdmin?account=' + formData.account + '&password=' + formData.password).then(function (res) {
          userstore.admin_id = res.data.data.id
          console.log(res.data.data.id)
          if (res.data.code === 200) {
            that.$router.push({name: 'notice'});
          }else {
            alert('登陆失败!!');
            return false;
          }
          console.log(res)
        })
      } else if (formData.role === "学生") {
        axios.get('/login/loginStu?account=' + this.formData.account + '&password=' + formData.password).then(function (res) {
          userstore.student_id = res.data.data.id
          console.log("Student_id:"+userstore.student_id)
          if (res.data.code === 200) {
            that.$router.push({name: 'index'});
          } else {
            alert('登陆失败!!');
            return false;
          }
          console.log(res)
        })
      } else if (formData.role === "教师") {
        axios.get('/login/loginTeacher?account=' + formData.account + '&password=' + formData.password).then(function (res) {
          userstore.teacher_id = res.data.data.id
          // console.log(res.data.data.id)
          if (res.data.code === 200) {
            that.$router.push({name: 'thome'});
          } else {
            alert('登陆失败!!');
            return false;
          }
          console.log(res)
        })
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.formData.role = ''
    }
  }
};
</script>
